package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.CustomerMapper;
import com.jt.pojo.Customer;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public List<Customer> findAll() {
        return customerMapper.selectList(null);
    }

    /**
     *分页查询
     */
    @Override
    public PageResult getCustomerList(PageResult pageResult) {
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        QueryWrapper<Customer> queryWrapper =new QueryWrapper<>();
        queryWrapper.like(flag,"name",pageResult.getQuery());

        //编辑mp的分页对象 四个属性有用(页数/条数/总数/记录) 传递= 页面/条数
        IPage<Customer> page = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        page = customerMapper.selectPage(page, queryWrapper);
        //获取总数
        long total = page.getTotal();
        //获取记录数
        List<Customer> rows = page.getRecords();
        //将数据封装
        return pageResult.setTotal(total).setRows(rows);
    }

    @Override
    @Transactional
    public void updateStatus(Customer customer) {
        customerMapper.updateById(customer);
    }


    @Override
    @Transactional
    public void deleteCustomerById(Integer id) {
        customerMapper.deleteById(id);
    }

    /**新增
     */
    @Override
    @Transactional //事务控制
    public void addCustomer(Customer customer){
        //SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        customer.setStatus(true).setCreated(new Date());//设定状态默认值
        customerMapper.insert(customer);

    }


    @Override
    public Customer getCustomerById(Integer id) {
        return customerMapper.selectById(id);
    }

    @Override
    @Transactional
    public void updateCustomer(Customer customer) {
        customerMapper.updateById(customer);
    }




}
